import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import 'package:kq_flutter_widgets/theme/kq_theme_manager.dart';
import 'package:kq_flutter_core_widget/utils/ex/string_ex.dart';
import 'package:kq_flutter_core_widget/widgets/button/kq_ink_well.dart';
import 'package:kq_flutter_widgets/widgets/wrapFlow/kq_auto_column_flow.dart';

import '../../resources/kq_theme_colors.dart';

class KqTxtCard extends StatefulWidget {
  /// 标题
  final String? title;

  ///最外围的边距
  final EdgeInsetsGeometry? margin;

  /// tags 标签
  final List<Widget>? tags;

  /// 卡片信息内容个数
  final int totalCount;

  /// 行数
  final int Function()? rowCountCallback;

  ///每行的列数
  final int Function(int row)? renderCrossAxisCountCallback;

  ///icon
  final String Function(int row, int column)? renderIconPathCallback;

  ///Text
  final String Function(int row, int column)? renderTextCallback;

  /// 指定位置的右边widget自定义
  final Widget Function(int row, int column)? renderWidgetCallback;

  ///Text行数
  final int Function(int row, int column) renderTextMaxLineCallback;

  /// 点击
  final void Function()? onTap;

  const KqTxtCard(
      {this.title,
      this.margin,
      this.tags,
      this.onTap,
      required this.totalCount,
      required this.renderCrossAxisCountCallback,
      this.renderIconPathCallback,
      this.renderTextCallback,
      this.renderWidgetCallback,
      required this.rowCountCallback,
      required this.renderTextMaxLineCallback,
      super.key});

  @override
  State<KqTxtCard> createState() => _KqTxtCardState();
}

class _KqTxtCardState extends State<KqTxtCard> {
  double widgetWidth = KqScreenUtil().screenWidth;

  /// 水平间距
  double spacing = 8.r;

  /// 垂直方向间距
  double runSpacing = 6.r;

  @override
  Widget build(BuildContext context) {
    return KqInkWell(
      onTap: () {
        widget.onTap?.call();
      },
      enableRipple: false,
      child: Container(
        color: KqThemeColors.bgWhite,
        padding: EdgeInsets.symmetric(horizontal: 16.r, vertical: 10.r),
        margin: widget.margin ??
            EdgeInsets.only(left: 10.r, right: 10.r, top: 10.r),
        child: renderContent(),
      ),
    );
  }

  Widget renderContent() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: [
        renderTitleInfo(),
        SizedBox(
          height: 10.r,
        ),
        renderWrapFlowContent(),
      ],
    );
  }

  List<Widget> renderTags() {
    List<Widget> tags = [];
    if (widget.tags != null) {
      for (var i = 0; i < widget.tags!.length; i++) {
        tags.add(widget.tags![i]);
        if (i != widget.tags!.length - 1) {
          tags.add(SizedBox(
            width: 6.r,
          ));
        }
      }
    }
    return tags;
  }

  Widget renderTitleInfo() {
    String title = widget.title ?? '';
    return Row(
      children: [
        Expanded(
            child: Text(title.ellipsis(),
                style: TextStyle(
                    fontSize:
                        KqThemeManager.getFormConfig().titleFontSize ?? 14.sp,
                    color: KqThemeColors.text26),
                maxLines: 1,
                overflow: TextOverflow.ellipsis)),
        Visibility(
            visible: widget.tags != null,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: renderTags(),
            )),
      ],
    );
  }

  Widget renderWrapFlowContent() {
    return KqAutoColumnFlow(
        spacing: spacing,
        runSpacing: runSpacing,
        fontSize: 12.r,
        iconWidth: 12.r,
        totalCount: widget.totalCount,
        renderCrossAxisCountCallback: widget.renderCrossAxisCountCallback,
        renderIconPathCallback: widget.renderIconPathCallback,
        renderTextCallback: widget.renderTextCallback,
        rowCountCallback: widget.rowCountCallback,
        renderWidgetCallback: widget.renderWidgetCallback,
        renderTextMaxLineCallback: widget.renderTextMaxLineCallback);
  }
}
